﻿using System;
using System.Runtime.InteropServices;

public class FileProperties
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct SHELLEXECUTEINFO
    {
        public int cbSize;
        public uint fMask;
        public IntPtr hwnd;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpVerb;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpFile;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpParameters;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpDirectory;
        public int nShow;
        public IntPtr hInstApp;
        public IntPtr lpIDList;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string lpClass;
        public IntPtr hkeyClass;
        public uint dwHotKey;
        public IntPtr hIcon;
        public IntPtr hProcess;
    }

    // 常量定义
    private const uint SEE_MASK_INVOKEIDLIST = 0x0000000C;
    private const uint SW_SHOW = 5;

    // 调用ShellExecuteEx函数
    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

    // 打开文件/文件夹属性
    public static void ShowFileProperties(string path)
    {
        SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
        info.cbSize = Marshal.SizeOf(info);
        info.lpVerb = "properties"; // 动作：打开属性页面
        info.lpFile = path; // 文件或文件夹的路径
        info.nShow = (int)SW_SHOW;
        info.fMask = SEE_MASK_INVOKEIDLIST;

        // 执行Shell操作
        if (!ShellExecuteEx(ref info))
        {
            throw new System.ComponentModel.Win32Exception();
        }
    }
}

